package com.safe.app.config;


import com.safe.app.utils.exceptions.enums.ErrorMsgEnum;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;


/**
 * 返回结果统一封装
 *
 * @author h2l
 */
@Data
public class ResultBean implements Serializable {

    private static final long serialVersionUID = 8632964679110167394L;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * 错误编号
     */
    private int code;
    /**
     * 验证码
     */
    private String verifyCode;
    /**
     * 业务返回值
     */
    private Object result;

    public ResultBean() {
        super();
        this.msg = ErrorMsgEnum.success.getMsg();
        this.code = ErrorMsgEnum.success.getCode();
    }

    public ResultBean(ErrorMsgEnum errorMsgEnum, String msg) {
        this.msg = msg;
        this.code = errorMsgEnum.getCode();
    }

    public ResultBean(ErrorMsgEnum msgEnum) {
        this.msg = msgEnum.getMsg();
        this.code = msgEnum.getCode();
    }

    public ResultBean(Object result) {
        this.msg = ErrorMsgEnum.success.getMsg();
        this.code = ErrorMsgEnum.success.getCode();
        if (result != null) {
            this.result = result;
        }
    }

    public ResultBean(boolean status) {
        this.msg = ErrorMsgEnum.success.getMsg();
        this.code = ErrorMsgEnum.success.getCode();
        this.result = status;
    }

    public ResultBean(String result, String msg) {
        if (StringUtils.isBlank(msg)) {
            this.msg = ErrorMsgEnum.success.getMsg();
        } else {
            this.msg = msg;
        }
        this.code = ErrorMsgEnum.success.getCode();
        this.result = result;
    }

    public ResultBean(String msg) {
        this.msg = msg;
        this.code = ErrorMsgEnum.success.getCode();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"msg\":\"").append(msg);
        sb.append("\",\"code\":\"").append(code);
        sb.append("\",\"result\":").append(result);
        sb.append("}");
        return sb.toString();
    }

}
